package com.cdqidi.core.weixinmanager.util;

import com.cdqidi.exception.ApiException;
import com.jfinal.weixin.sdk.api.ApiResult;
import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 * 〈一句话功能简述〉<br>
 *
 * @author sjkyll
 * @create 2018-08-24 11:35
 * @since 1.0.0
 */
@Slf4j
public class QrUtil {
    private QrUtil() {

    }

    /**
     * @param inputStream 二进制流
     * @param imgPath     图片的保存路径
     * @param imgName     图片的名称
     **/
    public static void saveToImgByInputStream(InputStream inputStream, String imgPath, String imgName) throws IOException {
        if (inputStream != null) {
            try (ByteArrayOutputStream byteArrayOutputStream = cloneInputStream(inputStream)) {
                assert byteArrayOutputStream != null;
                try (InputStream stream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray())) {
                    final int i = stream.read();
                    //123是{，标识出错了，InputStream 是JSON字符串
                    if (i == 123) {
                        byte[] b = new byte[1024];
                        int length = stream.read(b);
                        String s = "{" + new String(b, 0, length);
                        final ApiResult result = new ApiResult(s);
                        throw new ApiException("微信端异常，code: " + result.getErrorCode() + ",msg: " + result.getErrorMsg());
                    }
                }
                try (InputStream stream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray())) {
                    File file = new File(imgPath);
                    if (!file.exists()) {
                        boolean b = file.mkdirs();
                        log.info("创建文件夹，路径： {},状态： {}", imgPath, b);
                    }
                    file = new File(imgPath + File.separator + imgName);
                    log.info("生成图片路径: {},图片名称: {}", imgPath, imgName);
                    try (FileOutputStream fos = new FileOutputStream(file)) {
                        byte[] b = new byte[1024];
                        int nRead;
                        while ((nRead = stream.read(b)) != -1) {
                            fos.write(b, 0, nRead);
                        }
                        fos.flush();
                    }
                }
            }
        }
    }

    public static boolean isFileExists(String path, String fileName) {
        File file = new File(path, fileName);
        return file.exists();
    }

    private static ByteArrayOutputStream cloneInputStream(InputStream input) {
        try {
            try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
                byte[] buffer = new byte[1024];
                int len;
                while ((len = input.read(buffer)) > -1) {
                    byteArrayOutputStream.write(buffer, 0, len);
                }
                byteArrayOutputStream.flush();
                return byteArrayOutputStream;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}